<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;

class Spike extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('spike')
            ->setDescription('the spike command');
    }

    // 看视频回调加积分
    /**
     * 生成每天抢购的redis信息
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function execute(Input $input, Output $output)
    {
        $redis = Cache::store('redis');
        // 指令输出
        $goods_list = Db::name('commodity_spike')
            ->where('spike_num', '>', 0)
            ->select()
            ->toArray();
        foreach ($goods_list as $item) {
            $user_queue_key = "Goods_" . $item['id'] . "_User_" . date('Y-m-d', time());
            $goods_number_key = "Goods" . $item['id'] . '_' . date('Y-m-d', time());
            $getUserRedis = $redis->hGetAll("$user_queue_key");
            $gnRedis = $redis->llen("$goods_number_key");
            if (!count($getUserRedis) && !$gnRedis) {
                for ($i = 0; $i < $item['spike_num']; $i++) {
                    $redis->lpush("$goods_number_key", 1);
                }
            }
        }
        $output->writeln('spike');
    }
}
